package com.example.administrator.szb.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.InputType;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.base.BasePresenter;
import com.example.administrator.szb.activity.base.MVPBaseActivity;
import com.example.administrator.szb.http.HttpUtil;
import com.example.administrator.szb.http.HttpUtils;
import com.example.administrator.szb.http.OnResultListener;
import com.example.administrator.szb.http.URLAddress;
import com.example.administrator.szb.util.Contants;
import com.example.administrator.szb.util.SPUtils;
import com.example.administrator.szb.util.Toasts;

import java.util.HashMap;
import java.util.Map;

public class MyInfoEditActivity extends MVPBaseActivity {
    private int type;//所需要修改类型1呢称、2邮箱、3个性签名、4手机号5用户头像
    private TextView bar_title,bar_right;
    private EditText editText;
    private String phone;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_info_edit);
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void initView() {
        bar_title = (TextView) findViewById(R.id.bar_title);
        bar_right = (TextView) findViewById(R.id.bar_right);
        bar_right.setVisibility(View.VISIBLE);
        editText = (EditText) findViewById(R.id.editText);

    }

    @Override
    protected void initEvent() {
        bar_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (type==4){
                    //请求验证密码，跳转到手机号修改
                    requestMiMa();
                }else {
                    //请求数据修改
                    requestUpdate();
                }
            }
        });
    }

    @Override
    protected void initData() {
        type = getIntent().getIntExtra("type",1);
        phone = getIntent().getStringExtra("phone");
        String title = "修改";
        String str = "";
        switch (type){
            case 1:
                str="昵称";
                editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(6)});
                break;
            case 2:
                str="邮箱";
                editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)});
                break;
            case 3:
                str="个性签名";
                editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(15)});
                break;
            case 4:
                str="手机号";
                editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)});
                editText.setInputType(InputType.TYPE_CLASS_TEXT |InputType.TYPE_TEXT_VARIATION_PASSWORD);
                break;
        }
        bar_title.setText(title+str);
        editText.setHint("请输入"+ str);
        if (type==4){
            editText.setHint("请输入密码");
            bar_right.setText("下一步");
        }
    }

    private void requestUpdate(){
        String str = getStr();
        if (str==null){
            return;
        }
        Map<String,Object> params = new HashMap<>();
        params.put("code", HttpUtil.getParam());
        params.put("users_id", SPUtils.getUserId());
        params.put("content",str);
        params.put("type",type);
        HttpUtils.post(activity,URLAddress.URLBASE + URLAddress.UPDATE_USER, params, new OnResultListener() {
            @Override
            public void onSuccess(int requestCode, String str) {
                Toasts.show(context,str,1000);
                MyInfoActivity.isNeedRefresh=true;
                finish();
            }

            @Override
            public void onFailure(int requestCode, String str) {

            }
        });
    }

    private String getStr(){
        String str = editText.getText().toString().trim();
        if (Contants.isEmpty(str)){
            Toasts.show(context,"内容不能为空",1000);
            return null;
        }
        if (type==2){
            if (!Contants.isEmail(str)){
                Toasts.show(context,"邮箱格式不正确",1000);
                return null;
            }
        }
        if (type==4&&!Contants.passIsRight(str)){
            Toasts.show(context,"请输入6-20位数字或字母",1000);
            return null;
        }
        return str;
    }
    private void requestMiMa(){
        String str = getStr();
        if (str==null){
            return;
        }

        Map<String,Object> map = new HashMap<>();
        map.put("code", HttpUtil.getParam());
        map.put("tel", phone);
        map.put("type", "2");
        map.put("pass", str);

        // String json = GetJsonDataUtil.simpleMapToJsonStr(map);
        String url = URLAddress.URLBASE + URLAddress.URL_LOGIN;
        HttpUtils.post(activity,url, map, new OnResultListener() {
            @Override
            public void onSuccess(int requestCode, String str) {
                Toasts.show(context,"密码正确",1000);
                Intent intent = new Intent(context,TelUpdateActivity.class);
                startActivity(intent);
                finish();
            }

            @Override
            public void onFailure(int requestCode, String str) {
                Toasts.show(context,str,1000);
            }
        });
    }
}
